<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<div id="tagSourceWinConfig-div" class="easyui-window" title="关联资源配置"
	style="width: 96%; height: 96%;"
	data-options="iconCls:'icon-cog',modal:true,openAnimation:'fade',closeAnimation:'slide',cache:false,collapsible:false,minimizable:false,maximizable:false,resizable:false">
	<table id="tagSourceList" style="height: 100%; width: 100%;">

	</table>

	<div id="tagSourceListToolBars" style="height: auto">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			data-options="iconCls:'icon-remove',plain:true"
			onclick="tagSourceList_remove()">删除</a>|
		<!-- 			<a href="javascript:void(0)" -->
		<!-- 			class="easyui-linkbutton" -->
		<!-- 			data-options="iconCls:'icon-add',plain:true" -->
		<!-- 			onclick="tagSourceList_append()">添加</a>| -->

		<a href="javascript:void(0)" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit',plain:true"
			onclick="tagSourceList_edit()">编辑</a> | <a href="javascript:void(0)"
			class="easyui-linkbutton"
			data-options="iconCls:'icon-undo',plain:true"
			onclick="tagSourceList_undo()">取消</a>| <a href="javascript:void(0)"
			class="easyui-linkbutton"
			data-options="iconCls:'icon-save',plain:true"
			onclick="tagSourceList_accept()">保存</a>|<input
			id="tagSourceItemTypeQue" style="width: 100px;" />|<input
			id="tagSourceItemStatusQue" style="width: 100px;" />
	</div>

</div>

<script type="text/javascript">
	var tagSourceQueryParams = {
		itemType : 0,
		like_tag_name : '',
		status : 0,
		tagId : '${param.id}'
	};
	function tagSourceListDatagridReload() {
		try {
			tagSourceQueryParams.itemType = tagSourceItemTypeQue
					.combobox('getValue');
			tagSourceQueryParams.status = tagSourceItemStatusQue
					.combobox('getValue');

			// 			tagSourceQueryParams.like_tag_name = tagName.textbox('getValue');
			tagSourceListDatagrid.datagrid('options').queryParams = tagSourceQueryParams;
			tagSourceListDatagrid.datagrid('reload');
		} catch (e) {
			console.info(e)
		}
	}
</script>
<script type="text/javascript">
	var tagSourceItemStatusQue = undefined;
	var tagSourceItemTypeQue = undefined;
	var tagSourceSelectIndex = undefined;
	var tagSourceSelectRow = undefined;
	var tagSourceEditIndex = undefined;
	var tagSourceEditRow = undefined;
	var tagSourceListDatagrid = undefined;
	$(function() {

		tagSourceItemTypeQue = $('#tagSourceItemTypeQue').combobox({
			valueField : 'id',
			textField : 'text',
			data : [ {
				"id" : 0,
				"text" : "全部资源类型",
				"selected" : true
			}, {
				"id" : 1,
				"text" : "视频"
			}, {
				"id" : 2,
				"text" : "音频"
			}, {
				"id" : 3,
				"text" : "文本"
			} ],
			onChange : function(newValue, oldValue) {
				tagSourceListDatagridReload();
			}
		});

		tagSourceItemStatusQue = $('#tagSourceItemStatusQue').combobox({
			valueField : 'id',
			textField : 'text',
			data : [ {
				"id" : 0,
				"text" : "全部状态",
				"selected" : true
			}, {
				"id" : 1,
				"text" : "已发布"
			}, {
				"id" : 2,
				"text" : "已下线"
			}, {
				"id" : 3,
				"text" : "待发布"
			} ],
			onChange : function(newValue, oldValue) {
				tagSourceListDatagridReload();
			}
		});

		$('.easyui-linkbutton').linkbutton();

		var tagSourceWinConfig_div = $('#tagSourceWinConfig-div').window({
			onBeforeClose : function() {

			},
			onClose : function() {
				tagSourceWinConfig_div.panel('destroy');
			}
		});

		tagSourceListDatagrid = $('#tagSourceList')
				.datagrid(
						{
							url : '${baseUrl}/tag/item/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : tagSourceQueryParams,
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#tagSourceListToolBars',
							onLoadSuccess : function() {
								tagSourceSelectIndex = undefined;
								tagSourceSelectRow = undefined;
								tagSourceEditIndex = undefined;
								tagSourceEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (tagSourceEditIndex != undefined) {
									return false;
								} else {
									tagSourceSelectIndex = index;
									tagSourceSelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (tagSourceEditIndex == undefined) {
									tagSourceEditIndex = index;
									tagSourceEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
								var _ed = $(this).datagrid('getEditor', {
									index : index,
									field : 'statusCode'
								});
								row.statusMsg = $(_ed.target).combobox(
										'getText');
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												var img = '<img src="'
														+ row.item.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'title',
										title : '标题',
										width : 249,
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.title;
											} else {
												return value;
											}
										}
									},
									{
										field : 'tag',
										title : '标签',
										align : 'center',
										width : 200
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											return row.statusMsg;
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 0,
													statusMsg : '待发布'
												}, {
													statusCode : 1,
													statusMsg : '已发布'
												}, {
													statusCode : 2,
													statusMsg : '已下线'
												} ],
												required : true
											}
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 80,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemId',
										title : '资源ID',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.id;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemTypeCode',
										title : '资源类型',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.itemTypeMsg) {
												return row.itemTypeMsg;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '资源实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.entity;
											} else {
												return value;
											}
										}
									},
									{
										field : 'parent',
										title : '所属集合',
										width : 200,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item.parent) {
												return "<span>集合ID:"
														+ row.item.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.item.parent.title
														+ "</span>";
											} else {
												return value;
											}
										}
									},
									{
										field : 'summary',
										title : '简介',
										width : 70,
										hidden : true,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.summary;
											} else {
												return value;
											}
										}
									},
									{
										field : 'info',
										title : '详细介绍',
										hidden : true,
										width : 70,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.info;
											} else {
												return value;
											}
										}
									},
									{
										field : 'playtime',
										title : '播放时间(秒)',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.playtime;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemStatusCode',
										title : '资源状态',
										width : 70,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.statusMsg;
											} else {
												return value;
											}
										}
									} ] ],
							onDblClickRow : function(index, row) {
								var _url = '';
								if (row.itemTypeCode == 1) {
									_url = 'video';
								} else if (row.itemTypeCode == 2) {
									_url = 'audio';
								} else if (row.itemTypeCode == 3) {
									_url = 'text';
								}

								$.ajax({
									data : {
										id : row.item.id
									},
									async : false,
									url : baseUrl + '/' + _url + '/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		tagSourceListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							tagSourceListDatagrid.datagrid('gotoPage',
									tagSourceListDatagrid.datagrid('getPager')
											.find('.pagination-num').val());
						}
					} ]
				});

	});

	function tagSourceList_append() {

		$.ajax({
			url : baseUrl + '/tag/item/add.html?itemType=1',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function tagSourceList_edit() {

		var tagSourceSelect = tagSourceListDatagrid.datagrid('getSelected');
		if (tagSourceEditIndex != undefined
				&& tagSourceEditIndex != tagSourceSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (tagSourceSelect) {
			if (tagSourceSelectIndex == undefined) {
				tagSourceSelectIndex = tagSourceListDatagrid.datagrid(
						"getRowIndex", tagSourceSelect);
			}

			tagSourceListDatagrid.datagrid('beginEdit', tagSourceSelectIndex);
			tagSourceSelectRow = tagSourceSelect;
		}
	}

	function tagSourceList_undo() {
		if (tagSourceSelectIndex != undefined) {
			tagSourceListDatagrid.datagrid('rejectChanges');
			tagSourceSelectIndex = undefined;
			tagSourceSelectRow = undefined;
			tagSourceEditIndex = undefined;
			tagSourceEditRow = undefined;
		}
	}

	function tagSourceList_accept() {
		if (tagSourceSelectIndex != undefined) {
			if (tagSourceListDatagrid.datagrid('validateRow',
					tagSourceSelectIndex)) {

				//验证发布状态
				var _edStatusCode = tagSourceListDatagrid.datagrid('getEditor',
						{
							index : tagSourceSelectIndex,
							field : 'statusCode'
						});
				var _statusMsg = $(_edStatusCode.target).combobox('getText');
				if (_statusMsg != '已发布' && _statusMsg != '待发布'
						&& _statusMsg != '已下线') {
					$.messager.alert('提示', '未知状态', 'warning');
					return;
				}

				//获取排序
				var _edSort = tagSourceListDatagrid.datagrid('getEditor', {
					index : tagSourceSelectIndex,
					field : 'sort'
				});

				var _Sort = $(_edSort.target).val();

				var _Data = {
					id : tagSourceEditRow.id,
					sort : _Sort,
					status : $(_edStatusCode.target).combobox('getValue')
				};

				$.ajax({
					data : _Data,
					async : false,
					type : 'POST',
					url : baseUrl + '/tag/item/update',
					success : function(data) {
						if (data.responseCode == '000') {
							tagSourceListDatagrid.datagrid('endEdit',
									tagSourceSelectIndex);

							tagSourceListDatagrid.datagrid('acceptChanges');

							tagSourceSelectIndex = undefined;
							tagSourceSelectRow = undefined;
							tagSourceEditIndex = undefined;
							tagSourceEditRow = undefined;

							$.messager.alert('提示', '修改成功', 'info');
						} else {
							$.messager.alert('提示', data.responseMessage,
									'error');
						}
					}
				});

			}
		}
	}

	function tagSourceList_tagListPage() {
		var tagSourceSelect = tagSourceListDatagrid.datagrid('getSelected');
		if (tagSourceSelect) {
			$.ajax({
				data : {
					itemId : tagSourceSelect.item.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function tagSourceList_remove() {
		var tagSourceSelect = tagSourceListDatagrid.datagrid('getSelected');
		if (tagSourceSelect) {
			$.messager.confirm('确认', '您确认想要删除吗?', function(r) {
				if (r) {
					$.ajax({
						data : {
							id : tagSourceSelect.id
						},
						async : false,
						url : baseUrl + '/tag/item/delete',
						success : function(data) {
							if (data.responseCode == '000') {
								$.messager.alert('提示', "删除成功!", 'info');

								tagSourceListDatagrid
										.datagrid('deleteRow',
												tagSourceListDatagrid.datagrid(
														'getRowIndex',
														tagSourceSelect));

							} else {
								$.messager.alert('提示', data.responseMessage,
										'error');
							}
						}
					});
				}
			});
		}
	}
</script>
